<!--
    试算页面 公共插件

    参数：
    planId         方案ID
    op             'add'=新增;'view'=查看;'edit'=修改

    方法:
    backPage        返回结果集
 -->
<template>
    <div>
        <rui-page :defines="defines">
            <Form ref="proposalApplicationFrom" :model="productImplemente" :rules="proposalApplicationFormValidate" :label-width="100">
                <div class="basis-info">
                <Row>
                    <Col :xs="24" :md="24" :lg="16" :xl="12">
                        <FormItem label="上线编码" prop="onlineNumber">
                            <Input v-model="productImplemente.onlineNumber"
                                   v-bind:disabled='true' :autosize="true"
                                   type="text" :maxlength="32" style="width: 250px"
                                   placeholder="请输入"/>
                        </FormItem>
                    </Col>
                    <Col :xs="24" :md="24" :lg="16" :xl="12">
                        <FormItem label="上线名称" prop="onlineName" :required=true>
                            <Input v-model="productImplemente.onlineName"
                                   v-bind:disabled='isReadOnly' :autosize="true"
                                   type="text" :maxlength="128" style="width: 250px"
                                   placeholder="请输入"/>
                        </FormItem>
                    </Col>
                </Row>
                <!--<Row>-->
                    <!--<Col span="24">-->
                        <!--<FormItem label="上线背景" prop="objective" :required=true>-->
                            <!--<Input v-model="productImplemente.objective"-->
                                   <!--:disabled='isReadOnly' :autosize="true"-->
                                   <!--type="textarea"-->
                                   <!--placeholder="请输入"/>-->
                        <!--</FormItem>-->
                    <!--</Col>-->
                <!--</Row>-->
                <!--<Row>-->
                    <!--<Col span="24">-->
                        <!--<FormItem label="上线目的" prop="background" :required=true>-->
                            <!--<Input v-model="productImplemente.background"-->
                                   <!--:disabled='isReadOnly' :autosize="true"-->
                                   <!--type="textarea"-->
                                   <!--placeholder="请输入"/>-->
                        <!--</FormItem>-->
                    <!--</Col>-->
                <!--</Row>-->
                <!--<Row>-->
                    <!--<Col span="24">-->
                        <!--<FormItem label="同业政策" prop="interBankPolicy" :required=true>-->
                            <!--<Input v-model="productImplemente.interBankPolicy"-->
                                   <!--:disabled='isReadOnly' :autosize="true"-->
                                   <!--type="textarea" placeholder="请输入"/>-->
                        <!--</FormItem>-->
                    <!--</Col>-->
                <!--</Row>-->
                <!--<Row>-->
                    <!--<Col span="24">-->
                        <!--<FormItem label="风险对策" prop="riskCountermeasures" :required=true>-->
                            <!--<Input v-model="productImplemente.riskCountermeasures"-->
                                   <!--:disabled='isReadOnly' :autosize="true"-->
                                   <!--type="textarea" placeholder="请输入"/>-->
                        <!--</FormItem>-->
                    <!--</Col>-->
                <!--</Row>-->
                <!--<Row>-->
                    <!--<Col span="24">-->
                        <!--<FormItem label="方案设定" prop="schemeSetting" :required=true>-->
                            <!--<Input v-model="productImplemente.schemeSetting"-->
                                   <!--:disabled='isReadOnly' :autosize="true"-->
                                   <!--type="textarea"-->
                                   <!--placeholder="请输入"/>-->
                        <!--</FormItem>-->
                    <!--</Col>-->
                <!--</Row>-->
                </div>
                <Alert v-show="'view'!== op">
                    <Row style="text-align: center;">
                        <Button @click="save" icon="md-bookmark" :loading="submitLoading" type="primary">保存</Button>
                    </Row>
                </Alert>
            </Form>

            <rui-table
                :defineId="'productPlanDefine'"
                :fixedLeft="['action']"
                :showIndex="false"
                :select="false"
                :columns="planColumns"
                :slots="[{key:'action',slot:'action'},{key:'productName',slot:'productName'}]"
                @loadDatas="queryData"
                ref-query="searchFormManage"
                ref="productPlanTable"
            >
                <template slot="toolBar">
                    <Button @click="add" type="primary" icon="md-add"  v-show="'view'!== op">新增</Button>
                </template>
                <template slot="action" slot-scope="{ row }">
                    <div>
                        <Button type="error" size="small" @click="remove(row)"  v-show="'view'!== op">删除</Button>
                    </div>
                </template>
                <template slot-scope="{ row, index }" slot="productName">
                    <a @click="view(row)">
                        {{row.productName}}
                    </a>
                </template>
            </rui-table>

            <div>
                <commonApproveRecord :stageId="planId" v-if="'2'== implementeStatus"/>
            </div>
        </rui-page>
        <commonProductlan v-if="modalVisible" :status="productStatus" :modalTitle="modalTitle" @backInfo="backInfo" @backResult="backResult"/>
    </div>
</template>
<script>

    import productPlanDefine from "../product-plan/define/product-plan-table-define";

    import {
        save,
        delById,
        getProposalApplicationInfo,
        getProposalApplicationInfoTable,
        saveProductPlanImplemente,
    } from "@/projects/afs-core-business/api/afs-product/product-launch/productLaunch";

    import commonProductlan from "@/projects/afs-core-business/pages/product/common/common-product-plan.vue";
    import commonApproveRecord from "../common/common-approve-record.vue";

    export default {
        name: "productLaunchTabOne",
        components: {
            commonProductlan,
            commonApproveRecord,
        },
        props: {
            //方案ID
            planId:{
                type:String,
            },
            //状态
            op:{
                type:String,
                required: false,
                default: 'view'
            },
            //是否展示审批记录,默认不展示
            implementeStatus:{
                type:String,
                required: false,
                default: '0'
            },
        },
        data() {
            return {
                //表单
                productImplemente:{},
                //表单规则
                proposalApplicationFormValidate: {
                    // onlineNumber: [ {required: true, message: "不能为空",trigger: "blur"}],
                    onlineName: [ {required: true, message: "不能为空",trigger: "blur"}],
                    // objective: [ {required: true, message: "不能为空",trigger: "blur"}],
                    // background: [ {required: true, message: "不能为空",trigger: "blur"}],
                    // interBankPolicy: [ {required: true, message: "不能为空",trigger: "blur"}],
                    // riskCountermeasures: [ {required: true, message: "不能为空",trigger: "blur"}],
                    // schemeSetting: [ {required: true, message: "不能为空",trigger: "blur"}],
                },
                //按钮校验
                submitLoading: false,
                //只读
                isReadOnly: false,

                //列表显示字段
                planColumns: [],

                //弹窗
                productStatus:'2',
                modalTitle:'新增上线产品',
                modalVisible:false,
            }
        },
        methods: {
            init() {
                if ("view" == this.op) {
                    this.planColumns=['productNumber', 'productName', 'templateName', 'createTime'];
                    this.productImplemente.id = this.planId;
                    this.isReadOnly = true;
                    this.getProposalApplicationInfo();
                    this.refresh();
                }
                if ("edit" == this.op) {
                    this.planColumns=['action', 'productNumber', 'productName', 'templateName', 'createTime'];
                    this.productImplemente.id = this.planId;
                    this.isReadOnly = false;
                    this.getProposalApplicationInfo();
                    this.refresh();
                }
                if ("add" == this.op){
                    this.planColumns=['action', 'productNumber', 'productName', 'templateName', 'createTime'];
                    this.isReadOnly = false;
                }
            },
            getProposalApplicationInfo() {
                this.productImplemente.id = this.planId;
                getProposalApplicationInfo(this.productImplemente).then(res => {
                    if (res.code === "0000") {
                        this.productImplemente = res.data;
                    }
                });
            },
            //子组件校验，传递到父组件
            validateForm() {
                let flag = true;
                this.$refs.proposalApplicationFrom.validate(valid => {
                    if (valid) {
                        flag = true;
                    } else {
                        flag = false;
                    }
                })
                return flag;
            },
            //保存方案信息
            save() {
                this.$refs.proposalApplicationFrom.validate(valid => {
                    if (valid) {
                        this.submitLoading = true;
                        save(this.productImplemente).then(res => {
                            this.submitLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("操作成功");
                                this.productImplemente = res.data;
                                this.$emit('backResult',this.productImplemente.id);
                            }
                        }).catch(() => {
                            this.submitLoading = false;
                        });
                    }
                });
            },
            //列表初始化
            refresh() {
                this.$refs['productPlanTable'].reloadData();
            },
            //列表查询
            queryData(queryData) {
                queryData["id"]=this.productImplemente.id;
                getProposalApplicationInfoTable(queryData).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.productPlanTable.updateTableData(records, total);
                    }
                });
            },
            //查看产品详情
            view(v) {
                // console.info("查看的数据："+JSON.stringify(v))
                this.afs.newTab(
                    this,'projects/afs-core-business/pages/product/product-plan/productProposalInformationShow',
                    '产品方案详情查看',
                    'ios-add',
                    {status: '3', productTemplateId: v.productTemplateId, id: v.productPlanId, templateCategory: v.templateCategory,productInheritanceType:null,op:'view'},
                    v.id,[],
                    true
                );
            },
            //删除
            remove(row, index) {
                let self = this;
                self.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除?",
                    onOk: () => {
                        delById(row.id).then(res => {
                            if (res.code === "0000") {
                                this.refresh();
                            }
                        })
                    }
                });
            },
            //新增,打开弹窗
            add() {
                if (this.productImplemente.id == null || this.productImplemente.id == "") {
                    this.$Message.warning("请先保存上线信息！");
                    return;
                }else{
                    this.modalVisible = true;
                }
            },
            //关闭弹窗
            backInfo(){
                this.modalVisible = false;
            },
            //确定保存
            backResult(ids){
                this.productImplemente.ids = ids;
                this.submitLoading = true;
                saveProductPlanImplemente(this.productImplemente).then(res => {
                    this.submitLoading = false;
                    if (res.code === "0000") {
                        this.modalVisible = false;
                        this.refresh();
                    }
                }).catch(() => {
                    this.submitLoading = false;
                    this.modalVisible = false;
                });
            },
        },
        computed: {
            defines() {
                return [
                    {
                        id: "productPlanDefine",
                        fields: productPlanDefine
                    }
                ];
            }
        },
        mounted() {
            this.init();
        },
    };
</script>
<style scoped>
    .basis-info>>>.ivu-input[disabled]{
        color: black;
    }
    .basis-info>>>.ivu-select-disabled .ivu-select-selected-value{
        color:#515a6e;
    }
    .basis-info>>>.ivu-input-number-input[disabled]{
        color:black;
    }
</style>
